import { PureComponent } from "react";
import { isNull, handleSelect } from "../../utils/helper"
import MyRadioGroup from "./MyForm/MyRadioGroup"
interface Props {
	isReset: boolean,
	formData: object,
	dataset_key: string,
	getFieldValue: Function,
	setFieldsValue: Function,
	field: string[],
	field_mapping?: any,
	fd: object,
	getFieldDecorator: Function,
	defaultFormData: object,
	label?: string,
	required?: any,
	list: any[],
	value: any,
	onChange: Function,
	dispatch: Function,
	formDataKey: string
}
interface State {
	hasError: boolean,
	errorMsg: string
}
export default class MiRadioGroup extends PureComponent<Props, State> {
	public state: State = {
		hasError: false,
		errorMsg: ""
	}
	componentWillReceiveProps(next: Props) {
		let { isReset: oldReset, formData: oldFD } = this.props;
		let { isReset: newReset, formData: newFD, dataset_key, getFieldValue, setFieldsValue, field } = next;
		if (oldReset !== newReset && newReset) {
			this.setState({
				hasError: false
			})
		}
		if (oldFD[field[0]] !== newFD[field[0]]) {
			console.log(field, "变化了");
			const { formData, field_mapping = {} } = next;
			let list = next.fd[dataset_key] || [];
			let selectOption = list.filter(item => item.code === formData[field[0]]);
			let otherObj = handleSelect(field, selectOption[0], field_mapping);
			for (let i in otherObj) {
				getFieldValue(i);
				setFieldsValue({
					[i]: otherObj[i]
				})
			}
		}
	}

	componentDidMount() {
		setTimeout(() => {
			const { dataset_key, field, setFieldsValue, getFieldValue, fd, field_mapping = {} } = this.props;
			let _dataset_key = dataset_key;
			if (field.length == 1) {
				return;
			}
			const { formData } = this.props;
			let list = fd[_dataset_key] || [];
			let selectOption = list.filter(item => item.code === formData[field[0]]);
			let otherObj = handleSelect(field, selectOption[0], field_mapping);
			for (let i in otherObj) {
				getFieldValue(i);
				setFieldsValue({
					[i]: otherObj[i]
				})
			}
		}, 200)
	}
	render() {
		let { getFieldDecorator, field, defaultFormData, label, required, list } = this.props;
		if (list && list.length > 0) {
			list = list.map(i => {
				return {
					...i,
					label: i.name,
					value: i.code
				}
			})
		}
		list = [list]
		let { hasError, errorMsg } = this.state;
		let isRequired = required == 1;
		return (
			<div>
				{
					getFieldDecorator(field[0], {
						initialValue: defaultFormData[field[0]],
						rules: [
							{
								validator: (rule, value, callback) => {
									if (isRequired) {
										if (isNull(value)) {
											if (!hasError) {
												this.setState({
													hasError: true,
													errorMsg: "请选择：" + label
												})
												callback("请选择：" + label)
											}
										}
										else {
											this.setState({
												hasError: false,
												errorMsg: ""
											})
											callback()
										}
									}
									callback()
								}
							}
						]
					})(
						<MyRadioGroup {...this.props} hasError={hasError} errorMsg={errorMsg} />
					)
				}
			</div>
		)
	}
}
